function elemChildren(node) {
  var arr = [],
    childrens = node.childNodes,
    childLen = childrens.length;

  for (var i = 0; i < childLen; i++) {
    var childItem = childrens[i];

    if (childItem.nodeType === 1) {
      arr.push(childItem);
    }
  }

  return arr;
}

/**
 * 节点是否存在子元素节点
 * (补充方法)
 */
Node.prototype.hasChildElement = function () {
  var flag = false;
  if (this.hasChildNode) {
    var childs = this.childNodes,
      len = childs.length;
    for (var i = 0; i < len; i++) {
      if (childs[i].nodeType === 1) {
        flag = true;
        break;
      }
    }
  }
  return flag;
};

/**
 * 查找兄弟元素节点
 * 正数向后查找
 * 负数向前查找
 * 0为其本身
 * (补充方法)
 */
Node.prototype.brotherNode = function (index) {
  var node = this;
  while (index && node !== null) {
    if (index > 0) {
      node = node.nextSibling;
      if (node && node.nodeType === 1) {
        index--;
      }
    } else {
      node = node.previousSibling;
      if (node && node.nodeType === 1) {
        index++;
      }
    }
  }

  return node;
};

// 获取滚动条距离
function getScrollOffset() {
  if (window.pageXOffset) {
    return {
      left: window.pageXOffset,
      top: window.pageYOffset
    }
  } else {
    return {
      left: document.body.scrollLeft + document.documentElement.scrollLeft,
      top: document.body.scrollTop + document.documentElement.scrollTop
    }
  }
}

// 窗口高度
function getViewportSize() {
  if (window.innerWidth) {
    return {
      width: window.innerWidth,
      height: window.innerHeight
    }
  } else {
    if (document.compatMode === 'BackCompat') {
      return {
        width: document.body.clientWidth,
        height: document.body.clientHeight
      }
    } else {
      return {
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight
      }
    }
  }
}

// 文档高度
function getScrollSize() {
  if (document.body.scrollHeight) {
    return {
      width: document.body.scrollWidth,
      height: document.body.scrollHeight
    }
  } else {
    return {
      width: document.documentElement.scrollWidth,
      height: document.documentElement.scrollHeight
    }
  }
}

// 追加元素的事件绑定
function addEvent(el, type, fn) {
  if (el.addEventListener) {
    el.addEventListener(type, fn, false);
  } else if (el.attachEvent) {
    el.attachEvent('on' + type, fn);
  } else {
    el['on' + type] = fn;
  }
}

// 移除元素的事件绑定
function removeEvent(el, type, fn) {
  if (el.removeEventListener) {
    el.removeEventListener(type, fn, false);
  } else if (el.detachEvent) {
    el.detachEvent('on' + type, fn);
  } else {
    el['on' + type] = null;
  }
}

// 取消冒泡函数
function cancelBubble(e) {
  var e = e || window.event;
  if (e.stopPropagation) {
    e.stopPropagation();
  } else {
    e.cencelBubble = true;
  }
}

// 取消默认行为
function preventDefaultEvent(e) {
  var e = e || window.event;
  if (e.preventDefault) {
    e.preventDefault();
  } else {
    e.returnValue = false;
  }
}

// 元素的n级父元素
function elemParent(node, n) {
  var type = typeof (n);

  if (type === 'undefined') {
    return node.parentNode;
  } else if (n <= 0 || type !== 'number') {
    return undefined;
  }

  while (n) {
    node = node.parentNode;
    n--;
  }
  return node;
}

// （兼容性）当前文档位置
function pagePos(e) {
  var sLeft = getScrollOffset().left,
    sTop = getScrollOffset().top,
    cLeft = document.documentElement.clientLeft || 0,
    cTop = document.documentElement.clientTop || 0;

  return {
    X: e.clientX + sLeft - cLeft,
    Y: e.clientY + sTop - cTop,
  }
}

// 获取样式属性
function getStyles(elem, prop) {
  if (window.getComputedStyle) {
    console.log(elem, prop, window.getComputedStyle(elem, null)[prop])
    if (prop) {
      return parseInt(window.getComputedStyle(elem, null)[prop]);
    } else {
      return window.getComputedStyle(elem, null);
    }
  } else {
    if (prop) {
      return parseInt(elem.currentStyle[prop]);
    } else {
      return elem.currentStyle;
    }
  }
}

// 元素拖拽
function elemDrag(elem) {
  var x,
    y;
  addEvent(elem, 'mousedown', function (e) {
    var e = e || window.event;
    x = pagePos(e).X - getStyles(elem, 'left');
    y = pagePos(e).Y - getStyles(elem, 'top');
    addEvent(document, 'mousemove', mouseMove);
    addEvent(document, 'mouseup', mouseUp);
    cancelBubble(e);
    preventDefaultEvent(e);
  });
  function mouseMove(e) {
    var e = e || window.event;
    elem.style.left = pagePos(e).X - x + 'px';
    elem.style.top = pagePos(e).Y - y + 'px';
  }
  function mouseUp(e) {
    var e = e || window.event;
    removeEvent(document, 'mousemove', mouseMove);
    removeEvent(document, 'mouseup', mouseUp);
  }
}